<?php
class StudentsJobsController extends AppController {

    public $name = 'StudentsJobs';

    public function index(){
        $this->StudentsJob->recursive = 0;
        $this->set('studentsJobs', $this->paginate());
    }

    public function view($id = null){
        if (!$id){
            $this->Session->setFlash(__('Invalid StudentsJob.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('studentsJob', $this->StudentsJob->read(null, $id));
    }

    /* We fake an request for job application
     for it only takes 2 key parameters to create and application */
    public function add($job_id=null){
        if($job_id && !empty($this->user['student'])){
            $this->data['StudentsJob']['job_id'] = $job_id;
            $this->data['StudentsJob']['student_id'] = $this->user['student']['id'];
            if($this->StudentsJob->check($this->data['StudentsJob'])){
                $this->Response->json(false, __('You have applied for the job', true), $this->data);
            }else{
                $this->StudentsJob->create();
                if($this->StudentsJob->save($this->data)){
                    $this->Response->json(true, __('Your job apply request has been saved', true), $this->data);
                }else{
                    $this->Response->json(false, __('Your job apply request could not be saved. Please, try again.', true));
                }
            }
        }else{
            $this->Response->json(false, __('Invalid job apply request', true));
        }
    }

    public function edit($id = null){
        if (!$id && empty($this->data)){
            $this->Session->setFlash(__('Invalid StudentsJob', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)){
            if ($this->StudentsJob->save($this->data)){
                $this->Session->setFlash(__('The StudentsJob has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The StudentsJob could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)){
            $this->data = $this->StudentsJob->read(null, $id);
        }
        $students = $this->StudentsJob->Student->find('list');
        $jobs = $this->StudentsJob->Job->find('list');
        $this->set(compact('students','jobs'));
    }

    public function delete($job_id = null){
        if($job_id && !empty($this->user['student'])){
            $this->data['StudentsJob']['job_id'] = $job_id;
            $this->data['StudentsJob']['student_id'] = $this->user['student']['id'];
            $id = $this->StudentsJob->field('id', array(
                'student_id' => $this->user['student']['id'],
                'job_id' => $job_id
            ));
            if(!$this->StudentsJob->check($this->data['StudentsJob'])){
                $this->Response->json(false, __('You have not applied for the job', true), $id);
            }else{
                if($this->StudentsJob->del($id)){
                    $this->Response->json(true, __('Your job apply have been deleted', true), $id);
                }else{
                    $this->Response->json(false, __('Your job apply could not be deleted. Please, try again.', true));
                }
            }
        }else{
            $this->Response->json(false, __('Invalid job apply delete request', true));
        }
    }

}
?>